package com.foxmail.yusp75.myexam;

/*
Copyright 2017 Yu shanpeng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.afollestad.materialdialogs.MaterialDialog;

import java.util.Objects;

import io.realm.Realm;
import io.realm.RealmResults;

public class PaperViewActivity extends AppCompatActivity {

    private TextView txt_overview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_view);

        // toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        txt_overview = findViewById(R.id.overview_paper);

        Intent intent = new Intent();
        String paper_name = intent.getStringExtra("paper");

        // 生成显示题目
        new Content(paper_name).execute();

    }


    private class Content extends AsyncTask<Void, Integer, StringBuffer> {

        private MaterialDialog dialog;
        private String paper_name;

        public Content(String paper_name) {
            this.paper_name = paper_name;
        }

        @Override
        protected void onPreExecute() {

            txt_overview.setText("");
            Realm realm = Realm.getDefaultInstance();
            long num = realm.where(Question.class).equalTo("paper.name", paper_name).count() - 1;
            realm.close();

            dialog = new MaterialDialog.Builder(PaperViewActivity.this)
                    .title("生成数据")
                    .content("请稍等")
                    .progressIndeterminateStyle(true)
                    .progress(false, (int) num)
                    .show();
            super.onPreExecute();
        }

        @Override
        protected StringBuffer doInBackground(Void... params) {
            StringBuffer buffer = new StringBuffer();
            Realm realm = Realm.getDefaultInstance();
            try {
                RealmResults<Question> questions = realm.where(Question.class)
                        .equalTo("paper.name", paper_name)
                        .findAll();
                int i = 0;
                for (Question q : questions) {
                    buffer.append(q.getContent());
                    buffer.append("\n");
                    i++;
                    publishProgress(i);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                realm.close();
            }

            return buffer;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            //dialog.setProgress(values[0]);
            Log.d("我", Integer.toString(values[0]));
            super.onProgressUpdate(values);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(StringBuffer buffer) {
            txt_overview.setText(buffer.toString());
            dialog.dismiss();
            super.onPostExecute(buffer);
        }
    }

    // end
}
